package com.cloudsigns.factorymanager2.project.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品清单适配器
 * Created by tony on 2017/12/11.
 */

public class ProductListAdapter extends BaseAdapter implements View.OnTouchListener, View.OnFocusChangeListener {
    private Context context;
    private ArrayList<ProductBean> datas = new ArrayList<ProductBean>();
    private LayoutInflater inflater;
    private String tag = "";
    private ItemRemoveListener itemRemoveListener;
    private String selectedEditTextPosition = "";


    public ProductListAdapter(Context context, String tag) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.tag = tag;
    }

    public void setData(List<ProductBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }

    }

    public ArrayList<ProductBean> getData() {
        return datas;
    }

    public void setRemoveListener(ItemRemoveListener itemRemoveListener) {
        this.itemRemoveListener = itemRemoveListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public ProductBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ProductListAdapter.ViewHolder holder;
        if (convertView == null) {
            holder = new ProductListAdapter.ViewHolder();

            convertView = inflater.inflate(R.layout.item_product_list_lv, null);
            holder.img_left = convertView.findViewById(R.id.img_left);
            holder.edt_product_name = convertView.findViewById(R.id.edt_product_name);
            holder.edt_product_amount = convertView.findViewById(R.id.edt_product_amount);

            convertView.setTag(holder);
        } else {
            holder = (ProductListAdapter.ViewHolder) convertView.getTag();
        }

        ProductBean productBean = datas.get(position);
        if (null != productBean) {
            holder.edt_product_name.setText(productBean.getName());
            holder.edt_product_amount.setText(productBean.getAmount() + "");
        }

//        holder.edt_product_name.setOnTouchListener(this); // 正确写法
//        holder.edt_product_name.setOnFocusChangeListener(this);
//        holder.edt_product_name.setTag(position + "_1");
//
//        holder.edt_product_amount.setOnTouchListener(this); // 正确写法
//        holder.edt_product_amount.setOnFocusChangeListener(this);
//        holder.edt_product_amount.setTag(position + "_2");

//        if (!TextUtils.isEmpty(selectedEditTextPosition)) {
//            if (selectedEditTextPosition.equals(position + "_1")) {
//                holder.edt_product_name.requestFocus();
//                holder.edt_product_amount.clearFocus();
//            } else if (selectedEditTextPosition.equals(position + "_2")) {
//                holder.edt_product_name.clearFocus();
//                holder.edt_product_amount.requestFocus();
//            }
//        } else {
//            holder.edt_product_name.clearFocus();
//            holder.edt_product_amount.clearFocus();
//        }


        holder.img_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemRemoveListener != null) {
                    itemRemoveListener.remove(position);
                }
            }
        });


        return convertView;
    }

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
//            if (!TextUtils.isEmpty(selectedEditTextPosition)) {
//                int position = -1;
//                String[] str = selectedEditTextPosition.split("_");
//                if (null != str && str.length > 1) {
//                    position = Integer.parseInt(str[0]);
//                }
//
//                if (selectedEditTextPosition.contains("_1")) {
//
//                    if (position >= 0 && position < datas.size()) {
//                        ProductBean itemTest = (ProductBean) getItem(position);
//                        itemTest.setName(s.toString());
//                    }
//
//                } else if (selectedEditTextPosition.contains("_2")) {
//                    if (position >= 0 && position < datas.size()) {
//                        ProductBean itemTest = (ProductBean) getItem(position);
//                        String content = s.toString();
//                        if (!TextUtils.isEmpty(content)) {
//                            itemTest.setAmount(Integer.parseInt(content));
//                        }
//
//                    }
//                }
//
//
//            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

    @Override
    public boolean onTouch(View v, MotionEvent event) {
//        if (event.getAction() == MotionEvent.ACTION_UP) {
//            EditText editText = (EditText) v;
//            selectedEditTextPosition = (String) editText.getTag();
//        }
        return false;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
//        EditText editText = (EditText) v;
//        if (hasFocus) {
//            editText.addTextChangedListener(mTextWatcher);
//        } else {
//            editText.removeTextChangedListener(mTextWatcher);
//        }
    }


    private class ViewHolder {
        private ImageView img_left;
        private EditText edt_product_name;
        private EditText edt_product_amount;
    }


}
